package com.ingenio.siot.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONObject;

import com.ingenio.siot.Helper;
import com.ingenio.siot.adapter.TipoMovilAdapter;
import com.ingenio.siot.entity.TipoMovil;

/**
 * Servlet implementation class DevolverMovilServlet
 */
@WebServlet("/DevolverTipoMovil")
public class DevolverTipoMovilServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public DevolverTipoMovilServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			JSONObject json = new JSONObject();
			if(request.getParameter("id") != null && Helper.isInteger(request.getParameter("id").trim())){
				Integer id = Integer.parseInt(request.getParameter("id").trim());
				TipoMovilAdapter tma = new TipoMovilAdapter();
			    TipoMovil tm = tma.devolverTipoMovil(id);
				if(tm != null){
				  json.put("descripcion", tm.getDescTipoMovil());
				  json.put("densidad", tm.getDensidadPromedio());
				  if(tm.getUrlIcono() != null){
				    json.put("urlIcono", tm.getUrlIcono());
				  }else{
					json.put("urlIcono", "images/cono.gif");
				  }
				}else{
				  json.put("error", "No recupera tipo movil");
				}
			}else{
				json.put("error", "error id");
			}
			response.setContentType("application/json");
			response.getWriter().write(json.toString());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
